﻿using System;
using System.Threading;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Message;
namespace SLS.ExClassLib.WCFClient
{
    /// <summary>
    /// Class that holds everything that need to be binded to the main window
    /// </summary>
    public class ClientWFService
    {
        #region =================SINGLETON=================
        /// <summary>
        /// Singleton
        /// </summary>
        public static readonly ClientWFService Instance = new ClientWFService();
        public SynchronizationContext _uiSyncContext = null;
        /// <summary>
        /// Private constructor for singleton pattern
        /// </summary>
        private ClientWFService()
        {
           
        }
        #endregion

        #region =================PROPERTIES================= 
      
        public WorkFlowCenterServiceClient WFClient
        {
            get;
            set;

        }
        #endregion
 
        #region =================METHOD=================
        public void RegisterMessage(MessageEx msg)
        {
            EventDispatcher.Instance.MessagePool.Add(msg); 
        }

        public void SendWFMessage(MessageEx msgA)
        {
            try
            { 
                /*
                //触发消息的界面传入
                //TheMessage.Address = Address;
                msgA.MessageID_Return = msgA.MessageID;
                WorkFlow.ClientWFService.Instance.RegisterMessage(msgA);
                if (msgA.JobID != "OnThisFail" && !msgA.JobID.StartsWith("OnList"))
                {
                    msgA.setReturn = null; 
                }
                else
                {
                    if (msgA.setReturn != null)
                    {
                        msgA.setReturn = new DataSet();
                        foreach (DataTable dtTemp in msgA.setReturn.Tables)
                        {
                            if (dtTemp.TableName.Contains("QueryCond"))
                            {
                                msgA.setReturn = (DataSet)DataAccessService.Instance.ImportDataTable(msgA.setReturn, dtTemp);
                            }
                        }
                    }
                }
                msgA.setRequest = null;
                msgA.MessageID = msgA.MessageID;
                */
                ClientWFService.Instance.WFClient.WFService(msgA);
            }
            catch (Exception ex)
            {
                throw;
            }
        } 
        public void ProcessWF2UIMessage(MessageEx msg)
        {
            RegisterMessage(msg); 
            try
            {
                string JobID = msg.JobID;
                string JobType = msg.JobType;

                string JobContent = msg.JobContent;
                string Request = msg.Address;
                /*//sun 20121002
               SLBC doc = null;
               doc = (SLBC)DocumentManagement.Instance.FindDocumentByAddress(Request,"");
                if (doc != null)
                {
                    doc.ProcessWFMessage(msg);
                }*/
                return;
            }
            catch (Exception ex)
            {
                Tracer.Error("ClientWFService.ProcessWF2UIMessage", ex);
            }
        }
        #endregion
    }
}